package com.dxd.servlet.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dxd.constant.ErrorCodeConstant;
import com.dxd.entity.Message;
import com.dxd.entity.MessageBoard;
import com.dxd.entity.MessageType;
import com.dxd.entity.PageEntity;
import com.dxd.entity.ReturnBean;
import com.dxd.entity.UserInfo;
import com.dxd.servlet.PublicServlet;

/**
 * 消息管理
 * 
 * @author pppcar
 *
 */
@WebServlet("/auth/admin/message")
public class MessageServlet extends PublicServlet {

	private static final long serialVersionUID = -1093647922092449357L;
	private static final Logger LOG = LoggerFactory.getLogger(MessageServlet.class);

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String type = request.getParameter("type");
		if ("messageList".equals(type)) {// 查询所有的用户，包括分页，查询条件
			ReturnBean returnBean = ReturnBean.returnBeanBuild();
			try {
				String name = request.getParameter("title");// 根据标题
				String typeName = request.getParameter("type_name");// 父级id
				PageEntity<Message> data = messageDao.queryList(pageMap.get("page"), pageMap.get("size"), name,
						typeName);
				returnBean.setData(data);
			} catch (Exception e) {
				e.printStackTrace();
				LOG.error("查询信息失败，{}，{}", e.getMessage(), e);
				returnBean.setReturnCode(ErrorCodeConstant.ERROR).setReturnMsg("查询信息类型失败").setData(null);
			}
			returnJson(returnBean);
			return;
		} else if ("goMessageList".equals(type)) {
			Dispatcher("/view/admin/message_list.jsp");
		} else if ("addMessage".equals(type)) {
			ReturnBean returnBean = ReturnBean.returnBeanBuild();
			String title = request.getParameter("title");
			String imgs = request.getParameter("imgs");
			String messageType = request.getParameter("messageType");
			String createUse = request.getParameter("createUse");
			String address = request.getParameter("address");
			String state = request.getParameter("state");
			String lostTime = request.getParameter("lostTime");
			String detailContent = request.getParameter("detailContent");

			Message m = new Message();
			m.setTitle(title);
			m.setImgs(imgs);
			MessageType mt = new MessageType();
			mt.setId(messageType == null ? null : Integer.parseInt(messageType));
			m.setMessageType(mt);
			UserInfo u = new UserInfo();
			u.setId(createUse == null ? null : Integer.parseInt(createUse));
			m.setCreateUse(u);
			m.setAddress(address);
			m.setState(state == null ? null : Integer.parseInt(state));
			m.setLostTime(lostTime);
			m.setDetailContent(detailContent);

			String id = request.getParameter("id");

			if (id == null || "".equals(id)) {
				messageDao.addMessage(m);
				LOG.info("添加成功，名称：{}", title);
			} else {
				m.setId(id == null ? null : Integer.parseInt(id));
				messageDao.updateMessage(m);
				LOG.info("修改成功，名称：{}", title);
			}

			returnJson(returnBean);
			return;
		} else if ("deleteMessage".equals(type)) {
			ReturnBean returnBean = ReturnBean.returnBeanBuild();
			String ids = request.getParameter("ids");
			if (ids == null || "".equals(ids)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("参数为空");
				returnJson(returnBean);
				return;
			}
			messageDao.deleteBymessageIds(ids);
			LOG.info("删除成功，类型id：{}", ids);
			returnJson(returnBean);
			return;
		} else if ("audit".equals(type)) {
			ReturnBean returnBean = ReturnBean.returnBeanBuild();
			String id = request.getParameter("id");
			if (id == null || "".equals(id)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("参数为空");
				returnJson(returnBean);
				return;
			}
			Message message = messageDao.queryOne(Integer.parseInt(id));
			if (message == null || message.getId() == null) {
				returnBean.setReturnCode(ErrorCodeConstant.ERROR).setReturnMsg("消息不存在");
				returnJson(returnBean);
				return;
			}
			int type_id = message.getMessageType().getParent().getId();
			Integer state = type_id;

			messageDao.auditMessage(state, id);
			LOG.info("审核成功，id：{}", id);
			returnJson(returnBean);
			return;
		} else if ("queryOne".equals(type)) {
			ReturnBean returnBean = ReturnBean.returnBeanBuild();
			String id = request.getParameter("id");
			if (id == null || "".equals(id)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("参数为空");
				returnJson(returnBean);
				return;
			}
			Message message = messageDao.queryOne(Integer.parseInt(id));
			returnBean.setData(message);
			returnJson(returnBean);
			return;
		} else if ("queryBoardTest".equals(type)) {
			ReturnBean returnBean = ReturnBean.returnBeanBuild();
			String id = request.getParameter("id");
			if (id == null || "".equals(id)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("参数为空");
				returnJson(returnBean);
				return;
			}
			List<MessageBoard> messageBoards = messageBoardDao.queryByMessageId(Integer.parseInt(id));
			returnBean.setData(messageBoards);
			returnJson(returnBean);
			return;
		} else if ("queryBoard".equals(type)) {
			String id = request.getParameter("id");
			if (id == null || "".equals(id)) {
				id = "0";
			}
			List<MessageBoard> messageBoards = messageBoardDao.queryByMessageId(Integer.parseInt(id));
			request.setAttribute("list", messageBoards);
			Dispatcher("/view/admin/board_list.jsp");
		}
	}

}